package ui.activity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.github.barteksc.pdfviewer.PDFView;
import com.github.barteksc.pdfviewer.listener.OnFileDownloadCompleteListener;
import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener;
import com.github.barteksc.pdfviewer.listener.OnPageChangeListener;
import com.just.agentweb.AgentWeb;
import com.just.agentweb.AgentWebView;

import java.io.File;

import base.BaseBean;
import base.activity.BaseNetActivity;
import student.andlei.com.student.R;

public class CoursewareDetailsActivity extends BaseNetActivity{
    private AgentWeb mAgentWeb;
    private AgentWebView agentWebView;
    private PDFView pdfView;
    private String url;
    private int type;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("课件详情");

    }
    @Override
    public void success(String action, BaseBean baseBean) {

    }

    @Override
    public void error(String action, Throwable e) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_coursewaredetails;
    }

    @Override
    protected void findViews() {
        agentWebView = getView(R.id.AgentWebView);
        pdfView = getView(R.id.pdfView);
    }

    @Override
    protected void formatViews() {
        if(type != 6){
            pdfView.setVisibility(View.GONE);
            mAgentWeb = AgentWeb.with(CoursewareDetailsActivity.this)
                    .setAgentWebParent(agentWebView, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
                    .useDefaultIndicator()
                    .createAgentWeb()
                    .ready()
                    .go(url);
        }else {
            agentWebView.setVisibility(View.GONE);
            pdfView.setVisibility(View.GONE);
            //"http://www.anweitong.com/upload/document/standard/national_standards/138793918364316200.pdf"
            try{
                pdfView.fromUrl(url)
                        .enableSwipe(true) // allows to block changing pages using swipe
                        .defaultPage(0)
                        .onLoad(onLoadCompleteListener) // called after document is loaded and starts to be rendered
                        .onPageChange(onPageChangeListener)
                        .swipeHorizontal(false)
                        .enableAntialiasing(true)
                        .onFileDownload(onFileDownloadCompleteListener)
                        .loadFromUrl();
            }catch (Exception e){

            }

        }


//        pdfView.fromUrl("http://www.anweitong.com/upload/document/standard/national_standards/138793918364316200.pdf")
//                .enableSwipe(true) // allows to block changing pages using swipe
//                .defaultPage(0)
//                .onLoad(this) // called after document is loaded and starts to be rendered
//                .onPageChange(this)
//                .swipeHorizontal(false)
//                .enableAntialiasing(true)
//                .onFileDownload(this)
//                .loadFromUrl();
    }
    private OnLoadCompleteListener onLoadCompleteListener = new OnLoadCompleteListener() {
        @Override
        public void loadComplete(int nbPages) {
            pdfView.setVisibility(View.VISIBLE);
            if(nbPages>0){
                pdfView.setVisibility(View.VISIBLE);
            }else {
                toast("暂无课件信息，请稍后再试！");
            }
        }
    };
    private OnPageChangeListener onPageChangeListener = new OnPageChangeListener() {
        @Override
        public void onPageChanged(int page, int pageCount) {

        }
    };
    private OnFileDownloadCompleteListener onFileDownloadCompleteListener = new OnFileDownloadCompleteListener() {
        @Override
        public void onDownloadComplete(File file) {
            pdfView.setVisibility(View.VISIBLE);
        }
    };
    @Override
    protected void formatData() {

    }

    @Override
    protected void getBundle(Bundle bundle) {
        url = bundle.getString("url");
        type = bundle.getInt("type");
    }

    @Override
    public void onClick(View v) {

    }
}
